/**      
 * @file		selectionSort.cpp
 * @description	Demonstrates sorting an array with the selection
 *				sort algorithm.
 * @course		CSCI 123 Section 00000
 * @assignment 
 * @date		mm/dd/yyyy
 * @author		Brad Rippe (00000000) brippe@fullcoll.edu
 * @version		1.0
 */
#include <iostream>
#include <iomanip>
using namespace std;

const int ARRAY_SIZE = 10;

// leaving function comments out for brevity
void selectionSort(int aArray[], int aSize);
void swap(int& aElement1, int& aElement2);

/**
 * @return zero if the application executes successfully
 */
int main() {
	int a[ARRAY_SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
	
	cout << "Data items in original order\n";

	for(int i = 0; i < ARRAY_SIZE; i++) {
        	cout << setw( 4 ) << a[i];
	}

	selectionSort(a, ARRAY_SIZE); // sort the array

    cout << "\nData items in ascending order\n";
	
	for(int j = 0; j < ARRAY_SIZE;j++) {
        	cout << setw( 4 ) << a[j];
	}

    cout << endl;
	return 0;
}

void selectionSort(int aArray[], int aSize) {
	int smallest; 		// index of smallest element

	// loop over size - 1 elements
    for(int i = 0; i < aSize-1; i++) {
    	smallest = i; 	// first index of remaining array

        // loop to find index of smallest element
		// in the array
		for(int index = i+1; index < aSize; index++) {
			if(aArray[index] < aArray[smallest])
				smallest = index;
		}
        swap(aArray[i], aArray[smallest]);
	}
}

void swap(int& aElement1, int& aElement2) {
	int temp = aElement1;
	aElement1 = aElement2;
	aElement2 = temp;
}

